/*
 * Created by Jaren at 2022/1/27 10:50
 */
import 'package:inmotion_protocol/common/product_model.dart';

class DeviceUtils {
  // EzCAN 双轮系列设备号
  static const ezcanDulWheelPcDevice = 0x11;
  static const ezcanDulWheelMcDevice = 0x12;

  // EzCAN 独轮系列设备号
  static const ezcanVHmicDevice = 0x21;
  static const ezcanVMcDevice = 0x22;

  // EzCAN 滑板系列设备号
  static const ezcanScooterBtDevice = 0x31;
  static const ezcanScooterMcDevice = 0x32;

  // EzCAN 电动车系列设备号
  static const ezcanEBikeBtDevice = 0x31;
  static const ezcanEBikeMcDevice = 0x32;

  // Lorin 独轮系列设备号
  static const lorinVHmicDevice = 0x21;
  static const lorinVMcDevice = 0x22;
  static const lorinVBtDevice = 0x23;
  static const lorinVBMS1Device = 0x24;
  static const lorinVBMS2Device = 0x25;
  static const lorinVBMS3Device = 0x26;
  static const lorinVBMS4Device = 0x27;
  static const lorinVDisplay = 0x2E;
  static const lorinVTBox = 0x2F;

  // Lorin 滑板车系列设备号
  static const lorinScooterBtDevice = 0x32;
  static const lorinScooterMcDevice = 0x31;

  // Lorin LR1滑板车设备号
  static const lorinLR1BtDevice = 0x31;
  static const lorinLR1Mc1Device = 0x32;
  static const lorinLR1Mc2Device = 0x33;
  static const lorinLR1BmsDevice = 0x34;

  /// 根据产品型号[model]查询驱动板设备号
  /// 返回值为0表示当前型号没有驱动板
  static int getMcDevice(ProductModel model) {
    if (model.isLorinEuc()) return lorinVMcDevice;
    if (model.isLorinScooter()) return lorinScooterMcDevice;

    if (model.isEzcanEuc()) return ezcanVMcDevice;

    if (model.isL8Series()) return ezcanScooterMcDevice;

    if (model.isEBike()) return ezcanEBikeMcDevice;

    if (model.isDulWheel()) return ezcanDulWheelMcDevice;

    return 0;
  }

  /// 根据产品型号[model]查询人机板设备号
  /// 返回值为0表示当前型号没有人机板
  static int getHmicDevice(ProductModel model) {
    if (model.isLorinEuc()) return lorinVHmicDevice;
    if (model.isLorinScooter()) {
      if (model.isLR1Series() || model.isRSVeloSeries()) return lorinLR1BtDevice;
      return lorinScooterBtDevice;
    }

    if (model.isEzcanEuc()) return ezcanVHmicDevice;

    if (model.isDulWheel()) return ezcanDulWheelPcDevice;

    return 0;
  }

  /// 根据产品型号[model]查询蓝牙板设备号
  /// 返回值为0表示当前型号没有蓝牙板
  static int getBtDevice(ProductModel model) {
    if (model.isLorinEuc()) return lorinVBtDevice;
    if (model.isLorinScooter()) {
      if (model.isLR1Series() || model.isRSVeloSeries()) return lorinLR1BtDevice;
      return lorinScooterBtDevice;
    }

    if (model.isL8Series()) return ezcanScooterBtDevice;

    if (model.isEBike()) return ezcanEBikeBtDevice;

    if (model.isDulWheel()) return ezcanDulWheelPcDevice;

    return 0;
  }

  /// 获取所有BMS设备号
  static List<int> allBMSDevices(ProductModel model) {
    if (model.isV14Series()) {
      return [
        lorinVBMS1Device,
        lorinVBMS2Device,
        lorinVBMS3Device,
        lorinVBMS4Device
      ];
    }
    if (model.isV12sSeries() || model.isV9Series() || model.isP6Series()) {
      return [lorinVBMS1Device, lorinVBMS2Device];
    }
    if (model.isV13Series() || model.isV11Y()) {
      return [lorinVBMS1Device, lorinVBMS2Device];
    }
    if (model.isLR1Series() || model.isRSVeloSeries()) return [lorinLR1BmsDevice];
    return [];
  }

  /// 获取指定产品型号的全部设备列表
  static List<int> allDevices(ProductModel model) {
    if (model.isV11Y() || model.isV13Series()) {
      return [
        lorinVHmicDevice,
        lorinVMcDevice,
        lorinVBtDevice,
        lorinVBMS1Device,
        lorinVBMS2Device
      ];
    }
    if (model.isV11Series() || model.isV12Series()) {
      return [
        lorinVHmicDevice,
        lorinVMcDevice,
        lorinVBtDevice,
        lorinVBMS1Device,
        lorinVBMS2Device,
      ];
    }
    if (model.isV9Series()) {
      return [
        lorinVHmicDevice,
        lorinVMcDevice,
        lorinVBtDevice,
        lorinVBMS1Device,
        lorinVBMS2Device,
        lorinVTBox,
      ];
    }

    if (model.isV12sSeries()) {
      return [
        lorinVHmicDevice,
        lorinVMcDevice,
        lorinVBtDevice,
        lorinVBMS1Device,
        lorinVBMS2Device,
        lorinVDisplay,
        lorinVTBox,
      ];
    }
    if (model.isV14Series()) {
      return [
        lorinVHmicDevice,
        lorinVMcDevice,
        lorinVBtDevice,
        lorinVBMS1Device,
        lorinVBMS2Device,
        lorinVBMS3Device,
        lorinVBMS4Device
      ];
    }

    if (model.isP6Series()) {
      return [
        lorinVHmicDevice,
        lorinVMcDevice,
        lorinVBtDevice,
        lorinVBMS1Device,
        lorinVBMS2Device,
        lorinVDisplay,
        lorinVTBox,
      ];
    }

    if (model.isE20Series() || model.isV6Series() || model.isE10Series() || model.isE15Series()) {
      return [lorinVHmicDevice, lorinVMcDevice, lorinVBtDevice];
    }

    if (model.isLorinScooter()) {
      if (model.isLR1Series()) {
        return [
          lorinLR1BtDevice,
          lorinLR1Mc1Device,
          lorinLR1Mc2Device,
          lorinLR1BmsDevice
        ];
      }
      if (model.isRSVeloSeries()) {
        return [
          lorinLR1BtDevice,
          lorinLR1Mc1Device,
          lorinLR1Mc2Device,
          lorinLR1BmsDevice,
          lorinVTBox,
          lorinVDisplay
        ];
      }
      return [lorinScooterBtDevice, lorinScooterMcDevice];
    }

    if (model.isL8Series()) return [ezcanScooterBtDevice, ezcanScooterMcDevice];
    if (model.isV5Series() || model.isV8Series() || model.isV10Series()) {
      return [ezcanVHmicDevice, ezcanVMcDevice];
    }
    if (model.isEBike()) return [ezcanEBikeBtDevice];

    if (model.isDulWheel()) {
      return [ezcanDulWheelMcDevice, ezcanDulWheelPcDevice];
    }
    return [];
  }

  /// 根据型号获取对应设备名称标识
  static DeviceName getDeviceName(ProductModel model, int device) {
    if (model.isLorinEuc()) {
      if (device == lorinVHmicDevice) return DeviceName.hmicBoard;
      if (device == lorinVMcDevice) return DeviceName.driverBoard;
      if (device == lorinVBtDevice) return DeviceName.bleBoard;
      if (device == lorinVBMS1Device) return DeviceName.bms1Board;
      if (device == lorinVBMS2Device) return DeviceName.bms2Board;
      if (device == lorinVBMS3Device) return DeviceName.bms3Board;
      if (device == lorinVBMS4Device) return DeviceName.bms4Board;
      if (device == lorinVDisplay) return DeviceName.displayV18;
      if (device == lorinVTBox) return DeviceName.tBox;
      return DeviceName.unknownDevice;
    }
    if (model.isLorinScooter()) {
      if (model.isLR1Series() || model.isRSVeloSeries()) {
        if (device == lorinLR1BtDevice) return DeviceName.displayBoard;
        if (device == lorinLR1Mc1Device) return DeviceName.driver1Board;
        if (device == lorinLR1Mc2Device) return DeviceName.driver2Board;
        if (device == lorinLR1BmsDevice) return DeviceName.bmsBoard;
      }

      if (device == lorinScooterMcDevice) return DeviceName.driverBoard;
      if (device == lorinScooterBtDevice) return DeviceName.displayBoard;
      if (device == lorinVDisplay) return DeviceName.displayV18;
      if (device == lorinVTBox) return DeviceName.tBox;
      return DeviceName.unknownDevice;
    }
    if (model.isEzcanEuc()) {
      if (device == ezcanVHmicDevice) return DeviceName.hmicBoard;
      if (device == ezcanVMcDevice) return DeviceName.driverBoard;
      return DeviceName.unknownDevice;
    }
    if (model.isEzcanScooter()) {
      if (device == ezcanScooterMcDevice) return DeviceName.driverBoard;
      if (device == ezcanScooterBtDevice) return DeviceName.displayBoard;
      return DeviceName.unknownDevice;
    }
    if (model.isEBike()) {
      if (device == ezcanEBikeMcDevice) return DeviceName.driverBoard;
      if (device == ezcanEBikeBtDevice) return DeviceName.bleBoard;
      return DeviceName.unknownDevice;
    }
    if (model.isDulWheel()) {
      if (device == ezcanDulWheelPcDevice) return DeviceName.hmicBoard;
      if (device == ezcanDulWheelMcDevice) return DeviceName.driverBoard;
      return DeviceName.unknownDevice;
    }
    return DeviceName.unknownDevice;
  }
}

enum DeviceName {
  unknownDevice,
  hmicBoard,
  driverBoard,
  bleBoard,
  displayBoard,
  bmsBoard,
  bms1Board,
  bms2Board,
  bms3Board,
  bms4Board,
  driver1Board,
  driver2Board,
  displayV18,
  tBox,
}
